package org.example.rabbitmq.work.fair;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * @author zfj
 * @date 2022/2/21 -11:44
 */
public class Producer {
	public static void main(String[] args) {
		// 1: 创建连接工厂
		ConnectionFactory connectionFactory = new ConnectionFactory();
		// 2: 设置连接属性
		connectionFactory.setHost("127.0.0.1");
		connectionFactory.setPort(5672);
		connectionFactory.setVirtualHost("/");
		connectionFactory.setUsername("admin");
		connectionFactory.setPassword("admin");
		Connection connection = null;
		Channel channel = null;
		try {
			// 3: 从连接工厂中获取连接
			connection = connectionFactory.newConnection("生产者");
			// 4: 从连接中获取通道channel
			channel = connection.createChannel();
			// 6： 准备发送消息的内容
			//===============================end topic模式==================================
			for (int i = 1; i <= 20; i++) {
				//消息的内容
				String msg = "学相伴:" + i;
				// 7: 发送消息给中间件rabbitmq-server
				// @params1: 交换机exchange
				// @params2: 队列名称/routingkey
				// @params3: 属性配置
				// @params4: 发送消息的内容
				channel.basicPublish("", "queue1", null, msg.getBytes());
			}
			System.out.println("消息发送成功!");
		} catch (Exception ex) {
			ex.printStackTrace();
			System.out.println("发送消息出现异常...");
		} finally {
			// 7: 释放连接关闭通道
			if (channel != null && channel.isOpen()) {
				try {
					channel.close();
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}
		}
	}
}

